package com.jy.adscm.operation.lxcg.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.operation.lxcg.bean.LxcgMaterial;
import com.jy.adscm.operation.lxcg.bean.LxcgTask;
import com.jy.adscm.operation.lxcg.bean.LxcgTaskDetail;
import com.jy.adscm.operation.lxcg.mapper.mysql.LxcgMaterialMapper;
import com.jy.adscm.operation.lxcg.mapper.mysql.LxcgTaskDetailMapper;
import com.jy.adscm.operation.lxcg.mapper.mysql.LxcgTaskMapper;
import com.jy.adscm.operation.lxcg.service.LxcgTaskDetailService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 采购详情 服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-08-15
 */
@Service
public class LxcgTaskDetailServiceImpl extends ServiceImpl<LxcgTaskDetailMapper, LxcgTaskDetail> implements LxcgTaskDetailService {

    @Autowired
    JyUserMapper  jyUserMapper;

    @Autowired
    LxcgTaskMapper lxcgTaskMapper;

    @Override
    public boolean updateLxcgTaskDetail(LxcgTaskDetail lxcgTaskDetail, Token token) {
        DecimalFormat df=new DecimalFormat("#0.00");
        LxcgTaskDetail oldLxcgTaskDetail = super.selectById(lxcgTaskDetail.getId());
        if (AdscmBaseUtils.getInstance().isNotEmpty(lxcgTaskDetail.getPrice(),null)){
            if (AdscmBaseUtils.getInstance().isNotEmpty(lxcgTaskDetail.getNums(),null)){
                lxcgTaskDetail.setTotal(Float.valueOf(df.format(lxcgTaskDetail.getNums()*lxcgTaskDetail.getPrice())));
            }
        }
        if (! (AdscmBaseUtils.getInstance().isNotEmpty(oldLxcgTaskDetail.getActNums(),null)&&
                AdscmBaseUtils.getInstance().isNotEmpty(oldLxcgTaskDetail.getActPrice(),null))){
            if (AdscmBaseUtils.getInstance().isNotEmpty(lxcgTaskDetail.getActPrice(),null)){
                if (AdscmBaseUtils.getInstance().isNotEmpty(lxcgTaskDetail.getActNums(),null)){
                    lxcgTaskDetail.setActTotal(Float.valueOf(df.format(lxcgTaskDetail.getActNums()*lxcgTaskDetail.getActPrice())));
                    lxcgTaskDetail.setArriveTime(new Date());
                    lxcgTaskDetail.setIsArrive(1);
                    lxcgTaskDetail.setKfPerson(token.getUser().getId().toString());
                }
            }
        }
        super.updateById(lxcgTaskDetail);
        //查询是否已经完全都到货这个单子下面的
        Wrapper<LxcgTaskDetail> wrapper=new EntityWrapper<>();
        wrapper.eq("tid",lxcgTaskDetail.getTid());
        List<LxcgTaskDetail> lxcgTaskDetails = super.selectList(wrapper);
        int temp=0;
        for (LxcgTaskDetail taskDetail : lxcgTaskDetails) {
              if (taskDetail.getIsArrive()==0){
                  temp=1;
                  break;
              }
        }
        if (temp ==0){
            LxcgTask lxcgTask = lxcgTaskMapper.selectById(lxcgTaskDetail.getTid());
            lxcgTask.setIsArrive(1);
            lxcgTask.setArriveAddTime(new Date());
            lxcgTask.setArriveTime(new Date());
            lxcgTask.setKfPerson(token.getUser().getId().toString());
            lxcgTaskMapper.updateById(lxcgTask);
        }
        return true;
    }
    @Override
    public List<LxcgTaskDetail> selectByTid(Long tid) {
        Wrapper<LxcgTaskDetail> wrapper =new EntityWrapper<>();
        wrapper.eq("tid",tid);
        List<LxcgTaskDetail> lxcgTaskDetails = super.selectList(wrapper);
        for (LxcgTaskDetail lxcgTaskDetail : lxcgTaskDetails) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(lxcgTaskDetail.getCreator(),null)){
                JyUser jyUser = jyUserMapper.selectById(lxcgTaskDetail.getCreator());
                lxcgTaskDetail.setCreatorName(jyUser.getName());
            }
        }
        return lxcgTaskDetails;
    }
}
